﻿<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="renderer" content="webkit|ie-comp|ie-stand">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport"
              content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
        <meta http-equiv="Cache-Control" content="no-siteapp"/>
        <!--[if lt IE 9]>
        <script type="text/javascript" src="lib/html5shiv.js"></script>
        <script type="text/javascript" src="lib/respond.min.js"></script>
        <![endif]-->
        <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css"/>
        <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css"/>
        <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css"/>
        <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin"/>
        <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css"/>
        <!--[if IE 6]>
        <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
        <script>DD_belatedPNG.fix('*');</script>
        <![endif]-->
        <title>基金【每秒自动刷新数据】</title>
    </head>
    <body>
        <div class="page-container">
            <div class="text-c">
                <input type="text" class="input-text" style="width:900px" placeholder="输入基金编码，英文逗号隔开 如260108,040035,010020,009777,009147,005827,001679" id="fundCodeList"
                       name="fundCodeList">
                <button onclick="fundCodeListSubmit()" type="submit" class="btn btn-danger radius"
                        id="fundCodeListSubmit" name="fundCodeListSubmit"><i class="Hui-iconfont">&#xe630;
                </i> 提交
                </button>
            </div>
            <div class="cl pd-5 bg-1 bk-gray mt-20"><span class="l"><a href="javascript:;" class="btn btn-danger radius"
                                                                       id="sumFunds">今日估算总收益</a></span></div>
            <div class="mt-20">
                <table class="table table-border table-bordered table-hover table-bg table-sort">
                    <thead>
                        <tr class="text-c">
                            <th>编码</th>
                            <th>基金名称</th>
                            <th>估算净值</th>
                            <th>估算涨跌</th>
                            <th>更新时间</th>
                            <th>当日净值</th>
                            <th>持有份额</th>
                            <th>估算收益</th>
                        </tr>
                    </thead>
                    <tbody id="fundBody">
                    </tbody>
                </table>
            </div>
        </div>
        <!--_footer 作为公共模版分离出去-->
        <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
        <script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
        <script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->
        <!--请在下方写此页面业务相关的脚本-->
        <script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>
        <script type="text/javascript" src="lib/datatables/1.10.15/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
        <script type="text/javascript">
            $(function () {
                /*$('.table-sort').dataTable({
                    "aaSorting": [[3, "desc"]],//默认第几个排序
                    "bStateSave": false,//状态保存
                });*/
                $("#DataTables_Table_0_filter").hide();
                $("#DataTables_Table_0_info").hide();
                $("#fundCodeList").val($.cookie("fundCodeList"));
                fundCodeListSubmit();
            });
            
            function jsonpgz(data) {
                return data;
            }
            
            const url = "http://fundgz.1234567.com.cn/js/";
            
            function fundCodeListSubmit() {
                let codes = $("#fundCodeList").val();
                let codeArr = codes.split(",");
                let b = $("#fundBody");
                b.empty();
                for (let i = 0; i < codeArr.length; i++) {
                    let data = {};
                    data.fundcode = codeArr[i];
                    data.name = "--";
                    data.gsz = "--";
                    data.gszzl = "--";
                    data.gztime = "--";
                    data.dwjz = "--";
                    data.dwjz = "--";
                    data.jzrq = "--";
                    b.append(getFundTable(data));
                    //判断当前持有份额框是否已绑定失去焦点事件
                    let $cyfe = $("#cyfe" + data.fundcode);
                    $cyfe.blur(function () {
                        cSetCookie($(this).attr("id"), $(this).val());
                    });
                    $cyfe.val($.cookie($cyfe.attr("id")));
                    cSetCookie("fundCodeList", codes);
                }}
                
                function getFundTable(data) {
                    let h = "<tr class=\"text-c\" name=\"text-vc\" id=\"fundcode" +
                        data.fundcode +
                        "\">\n" +
                        "                            <td>" +
                        data.fundcode +
                        "</td>\n" +
                        "                            <td>" +
                        data.name +
                        "</td>\n" +
                        "                            <td>" +
                        data.gsz +
                        "</td>\n" +
                        "                            <td>" +
                        data.gszzl +
                        "</td>\n" +
                        "                            <td>" +
                        data.gztime +
                        "</td>\n" +
                        "                            <td>" +
                        data.dwjz +
                        "[" +
                        data.jzrq +
                        "]" +
                        "</td>\n" +
                        "                            <td><input type=\"text\" id=\"cyfe" +
                        data.fundcode +
                        "\" placeholder=\"持有份额\" class=\"input-text radius\"></td>\n" +
                        "                            <td name=\"oneFund\">0</td>\n" +
                        "                        </tr>";
                    return h;
                }
                
                function clock() {
                    let res = $("table tr[name='text-vc']");
                    let code = $($("td", res[0])[0]).text();
                    startFund(code,res,0);
                }
                
                function sumFund() {
                    let res = $("#sumFunds")
                    let tdSum = $("tr td[name='oneFund']");
                    if (tdSum.length === 0) {
                        return;
                    }
                    let ones = parseFloat("0");
                    for (let i = 0; i < tdSum.length; i++) {
                        ones += parseFloat($(tdSum[i]).text());
                    }
                    let hongt ="今日估算总收益 "+ ones.toFixed(4);
                    let hongc = "btn btn-danger radius"
                    let lvt ="今日估算总收益 "+ ones.toFixed(4);
                    let lvc = "btn btn-success radius"
                    if (ones > 0) {
                        res.attr("class", hongc);
                        res.html(hongt);
                    } else {
                        res.attr("class", lvc);
                        res.html(lvt);
                    }
                    
                }
                
                function startFund(code,res,i) {
                    if (i===res.length) {
                        return ;
                    }
                    $.ajax({
                        type: 'GET',
                        dataType: 'JSONP',
                        url: url + code + '.js',
                        // data: {rt:Math.round(new Date())},
                        jsonpCallback: 'jsonpgz',
                        async: false,
                        success: function (data) {
                            let newCode = data.fundcode
                            let newCodeDom = $("#fundcode" + newCode)
                            let $tdDom = $("td", newCodeDom);
                            $($tdDom[1]).text(data.name);
                            $($tdDom[2]).text(data.gsz);
                            let gszzl = data.gszzl;
                            if (parseFloat(gszzl) > 0) {
                                $($tdDom[3]).attr("style", "color: red;");
                                $($tdDom[7]).attr("style", "color: red;");
                            } else {
                                $($tdDom[3]).attr("style", "color: green;");
                                $($tdDom[7]).attr("style", "color: green;");
                            }
                            $($tdDom[3]).text(gszzl+"%");
                            $($tdDom[4]).text(data.gztime);
                            let x = data.gsz - data.dwjz;
                            let cyfe = $("input", $($tdDom[6])).val();
                            $($tdDom[5]).text(data.dwjz + "[" + data.jzrq + "]");
                            if (cyfe !== undefined && cyfe !== "") {
                                let v = x * cyfe;
                                $($tdDom[7]).text(v.toFixed(4));
                            }
                            i = i + 1;
                            startFund($($("td", res[i])[0]).text(),res,i)
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest.status);
                            console.log(XMLHttpRequest.readyState);
                            console.log(textStatus);
                        }
                    });
                }
                
                function cSetCookie(key, value) {
                    $.cookie(key, value, {expires: 99 * 365});
                }
                
                setInterval("clock()", 1000);
                setInterval("sumFund()", 1000);
        </script>
    </body>
</html>
